#include <hpdf.h>
#include <stdio.h>
#include <iostream>
#define INTEVAL 20
#define X_INTEVAL 20
#define Y_INTEVAL 20

#define START_X 100
#define START_Y 100

int main()
{
    // 创建一个新的PDF文档对象
    HPDF_Doc pdf = HPDF_New(NULL, NULL);
    if (!pdf)
    {
        printf("ERROR: cannot create pdf object.\n");
        return 1;
    }

    // 添加一个新页面到文档
    HPDF_Page page = HPDF_AddPage(pdf);

    // 设置页面大小（A4：宽210mm，高297mm）
    HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);

    // 获取字体并设置页面字体和大小
    HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", NULL);
    HPDF_Page_SetFontAndSize(page, font, 12);

    /*这行代码一定要放到移动文本位置之前*/
    HPDF_Page_BeginText(page);
    
    // 开始在页面上添加文本
    float width = HPDF_Page_GetWidth(page);
    float height = HPDF_Page_GetHeight(page);
    float x = START_X, y = height - START_Y;
    HPDF_Page_MoveTextPos(page, x, y);

    for(int k = 0; k < 5; k++)
    {
        for (int j = 0; j < 20; j++)
        {
            for (int i = 0; i < 10; i++)
            {
                HPDF_Page_ShowText(page, "a");
                HPDF_Page_MoveTextPos(page, X_INTEVAL, 0);
            }
            // 换行
            HPDF_Page_MoveTextPos(page, -10 * X_INTEVAL, -Y_INTEVAL);
        }
        if(k == 4)  break;
        HPDF_Page_EndText(page);
        page = HPDF_AddPage(pdf);
        HPDF_Page_BeginText(page);
        HPDF_Page_SetFontAndSize(page, font, 12);
        HPDF_Page_MoveTextPos(page, x, y);
    }
    
    // 将文档保存到文件
    const char *filename = "a.pdf";
    HPDF_SaveToFile(pdf, filename);

    // 释放文档对象所占用的资源
    HPDF_Free(pdf);

    return 0;
}